package com.gcu.common.network

import okhttp3.Interceptor
import okhttp3.Response

/**
 * 网络请求添加公共参数拦截器
 * @property mHeaderParamsMap HashMap<String, String>
 */
class HttpCommonInterceptor : Interceptor {

    private val mHeaderParamsMap = HashMap<String, String>()

    override fun intercept(chain: Interceptor.Chain): Response {
        val oldRequest = chain.request()
        val requestBuilder = oldRequest.newBuilder()
        requestBuilder.method(oldRequest.method(), oldRequest.body())
        //添加公共参数，添加到header中
        if (mHeaderParamsMap.size > 0) {

        }
        val newRequest = requestBuilder.build()
        return chain.proceed(newRequest)
    }

    companion object {
        class Builder {
            var mHttpCommonInterceptor = HttpCommonInterceptor()

            fun addHeaderParams(key: String, value: String): Builder {
                mHttpCommonInterceptor.mHeaderParamsMap[key] = value
                return this
            }

            fun addHeaderParams(key: String, value: Int): Builder {
                mHttpCommonInterceptor.mHeaderParamsMap[key] = value.toString()
                return this
            }

            fun addHeaderParams(key: String, value: Float): Builder {
                mHttpCommonInterceptor.mHeaderParamsMap[key] = value.toString()
                return this
            }

            fun addHeaderParams(key: String, value: Double): Builder {
                mHttpCommonInterceptor.mHeaderParamsMap[key] = value.toString()
                return this
            }

            fun build(): HttpCommonInterceptor {
                return mHttpCommonInterceptor
            }
        }
    }

}